/**
 * 试管架router
 */
import express from "express";
import { Schema } from "mongoose";
import {
  addData,
  updateData,
  findAll,
  deleteData,
  findById,
} from "../mdb/controlDB";
import {
  baseResp,
  workbenchModalname,
  workbenchSchematype,
  workbencSlotSchematype,
  expTableModalname,
  expTableSchematype,
} from "../common";

const router = express.Router();
const workbenchslotSchema = new Schema(workbencSlotSchematype);
const workbenchschema = new Schema(workbenchSchematype(workbenchslotSchema));

const expTableSchema = new Schema(expTableSchematype);

type TSlotInfo = {
  _id?: string;
  name: string;
  x: number;
  y: number;
};

type TWorkbench = {
  id?: string;
  _id?: string;
  name: string;
  row: number;
  column: number;
  rrow: number;
  rcolumn: number;
  slotList: TSlotInfo[];
};

type ITExpTable = {
  id?: string;
  _id?: string;
  name: string;
  desc: string;
  workbench: string;
  slotShelf: any;
  reactorSlotShelf: any;
};

// 新增工作台数据
router.post("/add", function (req, res) {
  const param = req.body;
  const addback = (data: any) => {
    if (data.success === false) {
      res.json(Object.assign({}, baseResp, data));
    } else {
      res.json(Object.assign({}, baseResp));
    }
  };

  addData({
    schematype: workbenchschema,
    callback: addback,
    modalname: workbenchModalname,
    param,
  });
});

// 获取所有工作台
router.get("/all", function (req, res) {
  const param = req.body;
  let temp: TWorkbench[] = [];
  const findback = (data: TWorkbench[]) => {
    if (data && data.length) {
      data.forEach((item: TWorkbench) => {
        const { name, row, column, rrow, rcolumn, slotList, _id } = item;
        temp.push({
          id: _id,
          name,
          row,
          column,
          rrow,
          rcolumn,
          slotList,
        });
      });
    }
    res.json(Object.assign({}, baseResp, { data: temp }));
  };

  findAll({
    schematype: workbenchschema,
    callback: findback,
    modalname: workbenchModalname,
    param,
  });
});

// 删除工作台
router.post("/del", function (req, res) {
  const param = req.body;
  const delback = (data: any) => {
    res.json(Object.assign({}, baseResp, data));
  };

  deleteData({
    schematype: workbenchschema,
    callback: delback,
    modalname: workbenchModalname,
    param,
  });
});

// 更新工作台
router.post("/update", (req, res) => {
  const param = req.body;
  const updateback = (data: any) => {
    res.json(Object.assign({}, baseResp, { data }));
  };
  updateData({
    schematype: workbenchschema,
    callback: updateback,
    modalname: workbenchModalname,
    param,
  });
});

// 详情
router.post("/detail", (req, res) => {
  const param = req.body;

  const findback = (data: any) => {
    if (data.success === false) {
      res.json(Object.assign({}, baseResp));
    } else {
      let tempData = null;
      if (data && data.length) {
        const { name, row, column, rrow, rcolumn, slotList, _id } = data[0];
        tempData = {
          id: _id,
          name,
          row,
          column,
          rrow,
          rcolumn,
          slotList,
        };
      }
      res.json(Object.assign({}, baseResp, { data: tempData }));
    }
  };

  findById({
    schematype: workbenchschema,
    callback: findback,
    modalname: workbenchModalname,
    param,
  });
});

// 获取所有预设实验台
router.get("/exptablelist", function (req, res) {
  const param = req.body;
  let temp: ITExpTable[] = [];
  const findback = (data: ITExpTable[]) => {
    if (data && data.length) {
      data.forEach((item: ITExpTable) => {
        const { name, desc, workbench, slotShelf, reactorSlotShelf, _id } =
          item;
        temp.push({
          id: _id,
          name,
          desc,
          workbench,
          slotShelf: JSON.parse(slotShelf),
          reactorSlotShelf: JSON.parse(reactorSlotShelf),
        });
      });
    }
    res.json(Object.assign({}, baseResp, { data: temp }));
  };

  findAll({
    schematype: expTableSchema,
    callback: findback,
    modalname: expTableModalname,
    param,
  });
});

// 新增预设工作台
router.post("/addexptable", (req, res) => {
  const { name, desc, workbench, slotShelf, reactorSlotShelf } = req.body;

  const addback = (data: any) => {
    if (data.success === false) {
      res.json(Object.assign({}, baseResp, data));
    } else {
      res.json(Object.assign({}, baseResp));
    }
  };

  const slotShelfStr = JSON.stringify(slotShelf);
  const reactorSlotShelfStr = JSON.stringify(reactorSlotShelf);

  addData({
    schematype: expTableSchema,
    callback: addback,
    modalname: expTableModalname,
    param: {
      name,
      desc,
      workbench,
      slotShelf: slotShelfStr,
      reactorSlotShelf: reactorSlotShelfStr,
    },
  });
});

// 删除预设实验台
router.post("/delexptable", (req, res) => {
  const param = req.body;
  const delback = (data: any) => {
    res.json(Object.assign({}, baseResp));
  };

  deleteData({
    schematype: expTableSchema,
    callback: delback,
    modalname: expTableModalname,
    param,
  });
});

// 更新预设实验台 updateexptable
router.post("/updateexptable", (req, res) => {
  const param = req.body;
  const { slotShelf, reactorSlotShelf } = param;
  const updateback = (data: any) => {
    res.json(Object.assign({}, baseResp));
  };

  const slotShelfStr = JSON.stringify(slotShelf);
  const reactorSlotShelfStr = JSON.stringify(reactorSlotShelf);

  updateData({
    schematype: expTableSchema,
    callback: updateback,
    modalname: expTableModalname,
    param: {
      ...param,
      slotShelf: slotShelfStr,
      reactorSlotShelf: reactorSlotShelfStr,
    },
  });
});

export default router;
